package com.demo.jinjunhuang.viewsdemo;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TableLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private TabLayout tabLayout;
    private ViewPager viewPager;

    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tabLayout = (TabLayout) findViewById(R.id.main_tablayout);
        viewPager = (ViewPager) findViewById(R.id.main_vp);

        initFragments();

        viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
        tabLayout.setupWithViewPager(viewPager);

    }

    private void initFragments() {
        fragments.add(MainFragment.newInstance(R.layout.linear_gradient_fragment, "LinearGradient"));
        fragments.add(MainFragment.newInstance(R.layout.radial_gradient_fragment, "RadialGradient"));
        fragments.add(MainFragment.newInstance(R.layout.sweep_gradient_fragment, "SweepGradient"));
        fragments.add(MainFragment.newInstance(R.layout.bitmap_shader_fragment, "BitmapShader"));
        fragments.add(MainFragment.newInstance(R.layout.compose_shader_fragment, "ComposeShader"));
        fragments.add(MainFragment.newInstance(R.layout.stroke_fragment, "stroke"));
        fragments.add(MainFragment.newInstance(R.layout.path_effect_fragment, "PathEffect"));
        fragments.add(MainFragment.newInstance(R.layout.corner_path_effect_fragment, "CornerEffect"));
        fragments.add(MainFragment.newInstance(R.layout.shadow_layer_fragment, "ShadowLayer"));
        fragments.add(MainFragment.newInstance(R.layout.draw_text_on_path_fragment, "DrawTextOnPath"));
        fragments.add(MainFragment.newInstance(R.layout.clip_view_fragment, "ClipView"));
        fragments.add(MainFragment.newInstance(R.layout.translate_fragment, "Translate"));
        fragments.add(MainFragment.newInstance(R.layout.rotate_fragment, "Rotate"));
        fragments.add(MainFragment.newInstance(R.layout.scale_fragment, "Scale"));
        fragments.add(MainFragment.newInstance(R.layout.skew_fragment, "Skew"));
        fragments.add(MainFragment.newInstance(R.layout.camera_rotate_fragment, "camera.rotate"));
        fragments.add(MainFragment.newInstance(R.layout.flipboard_fragment,"flipboard"));
        fragments.add(MainFragment.newInstance(R.layout.camera_rotate_anim_fragment,"rotate_anim"));
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return fragments.get(position).getArguments().getString("name");
        }
    }
}

